import { createRouter, createWebHistory } from 'vue-router'
import { useAdminStore } from '@/stores/admin.js'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    //直接跳到后台登录页面
    { path:'/',redirect: '/login'},
    { 
        path: '/login',
        meta: { title: '管理端登录页面' }, 
        component: () => import('@/views/Login.vue') 
    },
    {
        path: '/layout', 
        component: () => import('@/views/Layout.vue'),
        meta: { title: '管理控制台' }
    },
    // 用户管理
    {
        path: '/user',
        name: 'User',
        component: () => import('@/views/user/index.vue'),
        meta: { 
            title: '用户管理',
            permission: 'manageUsers'
        },
        children: [
            {
                path: 'audit',
                name: 'UserAudit',
                component: () => import('@/views/user/audit.vue'),
                meta: { 
                    title: '用户注册审核',
                }
            },
            {
                path: 'list',
                name: 'UserList',
                component: () => import('@/views/user/list.vue'),
                meta: { 
                    title: '用户列表'
                }
            },
            {
                path: 'edit',
                name: 'UserEdit',
                component: () => import('@/views/user/edit.vue'),
                meta: { 
                    title: '修改用户信息'
                }
            }
        ]
    },
    // 提案管理
    {
        path: '/proposal',
        name: 'Proposal',
        component: () => import('@/views/proposal/index.vue'),
        meta: { 
            title: '提案管理',
            permission: 'managePropositions'
        },
        children: [
            {
                path: 'first-audit',
                name: 'ProposalFirstAudit',
                component: () => import('@/views/proposal/first-audit.vue'),
                meta: { 
                    title: '提案初审'
                }
            },
            {
                path: 'shunt',
                name: 'ProposalShunt',
                component: () => import('@/views/proposal/shunt.vue'),
                meta: { 
                    title: '分流处理'
                }
            },
            {
                path: 'reply',
                name: 'ProposalReply',
                component: () => import('@/views/proposal/reply.vue'),
                meta: { 
                    title: '最终批复'
                }
            },
            {
                path: 'recheck',
                name: 'ProposalRecheck',
                component: () => import('@/views/proposal/recheck.vue'),
                meta: { 
                    title: '提案复审'
                }
            },
            {
                path: 'supervision',
                name: 'ProposalSupervision',
                component: () => import('@/views/proposal/supervision.vue'),
                meta: { 
                    title: '提案督办'
                }
            },
            {
                path: 'query',
                name: 'ProposalQuery',
                component: () => import('@/views/proposal/query.vue'),
                meta: { 
                    title: '提案查询'
                }
            }
        ]
    },
    // 提案专项处理
    {
        path: '/proposal-process',
        name: 'ProposalProcess',
        component: () => import('@/views/proposal-process/index.vue'),
        meta: { 
            title: '提案专项处理',
            permission: 'managePropositions'
        }
    },
    // 社情民意管理
    {
        path: '/public',
        name: 'Public',
        component: () => import('@/views/public/index.vue'),
        meta: { 
            title: '社情民意管理',
            permission: 'managePublic'
        },
        children: [
            {
                path: 'first-audit',
                name: 'PublicFirstAudit',
                component: () => import('@/views/public/first-audit.vue'),
                meta: { 
                    title: '社情民意初审'
                }
            },
            {
                path: 'shunt',
                name: 'PublicShunt',
                component: () => import('@/views/public/shunt.vue'),
                meta: { 
                    title: '分流处理'
                }
            },
            {
                path: 'reply',
                name: 'PublicReply',
                component: () => import('@/views/public/reply.vue'),
                meta: { 
                    title: '最终批复'
                }
            },
            {
                path: 'recheck',
                name: 'PublicRecheck',
                component: () => import('@/views/public/recheck.vue'),
                meta: { 
                    title: '社情民意复审'
                }
            },
            {
                path: 'supervision',
                name: 'PublicSupervision',
                component: () => import('@/views/public/supervision.vue'),
                meta: { 
                    title: '社情民意督办'
                }
            },
            {
                path: 'query',
                name: 'PublicQuery',
                component: () => import('@/views/public/query.vue'),
                meta: { 
                    title: '社情民意查询'
                }
            }
        ]
    },
    // 社情民意专项处理
    {
        path: '/public-process',
        name: 'PublicProcess',
        component: () => import('@/views/public-process/index.vue'),
        meta: { 
            title: '社情民意专项处理',
            permission: 'managePublic'
        }
    },
    // 社情民意详情
    {
        path: '/public/detail/:id',
        name: 'PublicDetail',
        component: () => import('@/views/public/detail.vue'),
        meta: { 
            title: '社情民意详情',
            permission: 'managePublic'
        }
    },
    // 会议管理
    {
        path: '/meeting',
        name: 'Meeting',
        component: () => import('@/views/meeting/index.vue'),
        meta: { 
            title: '会议管理',
            permission: 'manageMeetings'
        },
        children: [
            {
                path: 'add',
                name: 'MeetingAdd',
                component: () => import('@/views/meeting/addMeeting.vue'),
            },{
                path: 'list',
                name: 'MeetingList',
                component: () => import('@/views/meeting/meetingList.vue'),
                meta: { 
                    title: '会议列表'
                },
                children: [
                    {
                        path: 'overList',
                        name: 'MeetingOverList',
                        component: () => import('@/views/meeting/overList.vue'),
                        meta: { 
                            title: '结束的会议'
                        }
                    }
                ]
            },{
                path: 'sign/:mid',
                name: 'MeetingSign',
                component: () => import('@/views/meeting/meetingSign.vue')
            },{
                path: 'detail/:mid',
                name: 'MeetingDetail',
                component: () => import('@/views/meeting/meetingDetail.vue')
            }
        ]
    },
    // 活动管理
    {
        path: '/activity',
        name: 'Activity',
        component: () => import('@/views/activity/index.vue'),
        meta: { 
            title: '活动管理',
            permission: 'manageEvents'
        },
        children: [
            {
                path: 'audit',
                name: 'ActivityAudit',
                component: () => import('@/views/activity/audit.vue'),
                meta: { 
                    title: '活动审核'
                }
            },
            {
                path: 'outcomes',
                name: 'ActivityOutcomes',
                component: () => import('@/views/activity/outcomes.vue'),
                meta: { 
                    title: '活动成果'
                },
            },
            {
                path: '/activity/info/:aid',
                name: 'ActivityInfo',
                component: () => import('@/views/activity/activityInfo.vue')
            },
            {
                path: '/outcome/info/:oid',
                name: 'OutcomeInfo',
                component: () => import('@/views/activity/outcomeDetail.vue')
            }
                
        ]
    },
    // 签到管理
    {
        path: '/sign',
        name: 'Sign',
        component: () => import('@/views/sign/index.vue'),
        meta: { 
            title: '签到管理',
            permission: 'manageCheckIns'
        },
        children: [
            {
                path: 'add',
                name: 'SignAdd', 
                component: () => import('@/views/sign/addSign.vue'),
                meta: {
                    title: '发起签到'
                }
            },
            {
                path: 'endList',
                name: 'SignEndList',
                component: () => import('@/views/sign/endList.vue'),
                meta: { 
                    title: '结束的签到'
                }
            },
            {
                path: 'detail/:sid',
                name: 'SignDetail',
                component: () => import('@/views/sign/signDetail.vue'),
                meta: {
                    title: '签到情况'
                }
            },
            {
                path: 'data/:sid',
                name: 'SignData',
                component: () => import('@/views/sign/dataDetail.vue'),
                meta: {
                    title: '签到详情'
                }
            }
        ]
    },
    // 新闻发布
    {
        path: '/news',
        name: 'News',
        component: () => import('@/views/news/index.vue'),
        meta: { 
            title: '新闻发布',
            permission: 'publishNews'
        },
        children: [
            {
                path: '/add',
                name: 'newsAdd',
                component: () => import('@/views/news/addNews.vue'),
                meta: { 
                    title: '新闻发布'
                }
            },
            {
                path: 'edit/:id',
                name: 'newsEdit',
                component: () => import('@/views/news/editNews.vue'),
                meta: { 
                    title: '新闻修改'
                }
            }
        ]
    },
    // 主页编辑
    {
        path: '/topic',
        name: 'Topic',
        component: () => import('@/views/topic/index.vue'),
        meta: { 
            title: '主页编辑',
            permission: 'editHomePage'
        }
    },
    {
        path: '/proposal/detail/:id',
        name: 'ProposalDetail',
        component: () => import('@/views/proposal/proposal-detail.vue')
    }
  ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 检查是否需要权限
  if (to.meta.permission) {
    const adminStore = useAdminStore()
    // 检查是否有权限
    if (adminStore.hasPermission(to.meta.permission)) {
      next()
    } else {
      ElMessage.warning('您没有权限执行该操作，请联系超级管理员！')
      next('/layout')
    }
  } else {
    next()
  }
})

//导出路由
export default router 